package com.wxb.controller;

import com.wxb.common.core.web.domain.AjaxResult;
import com.wxb.domain.MallCategoryEntity;
import com.wxb.service.IMallCategoryService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Date 2025-05-30 16:40
 * @Version 1.0
 * @Description
 * @ClassName MallCatalogController
 **/
@RestController
@RequestMapping("/catalog")
public class MallCatalogController {
    @Autowired
    private IMallCategoryService categoryService;

    /**
     * 获取分类栏目数据
     */
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "id", paramType = "query", required = false),
            @ApiImplicitParam(name = "page", value = "page", paramType = "query", required = false),
            @ApiImplicitParam(name = "size", value = "size", paramType = "query", required = false)})
    @PostMapping(value = "index")
    public Object index(Integer id,
                        @RequestParam(value = "page", defaultValue = "1") Integer page,
                        @RequestParam(value = "size", defaultValue = "10") Integer size) {
        Map<String, Object> resultObj = new HashMap<>();
        Map<String, Object> params = new HashMap<>();
        params.put("page", page);
        params.put("limit", size);
        params.put("sidx", "sort_order");
        params.put("order", "asc");
        params.put("parentId", 0);
        //查询列表数据
        List<MallCategoryEntity> data = categoryService.queryList(params);
        //
        MallCategoryEntity currentCategory = null;
        if (null != id) {
            currentCategory = categoryService.queryObject(id);
        }
        if (null == currentCategory && null != data && data.size() != 0) {
            currentCategory = data.get(0);
        } else {
            currentCategory = new MallCategoryEntity();
        }

        //获取子分类数据
        if (null != currentCategory && null != currentCategory.getId()) {
            params.put("parentId", currentCategory.getId());
            currentCategory.setSubCategoryList(categoryService.queryList(params));
        }

        resultObj.put("categoryList", data);
        resultObj.put("currentCategory", currentCategory);
        return AjaxResult.success(resultObj);
    }

    /**
     *
     */
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "id", paramType = "query", required = false)})
    @GetMapping(value = "current")
    public Object current( @RequestParam(value = "id") Integer id) {
        Map<String, Object> resultObj = new HashMap<>();
        Map<String, Object> params = new HashMap<>();
        params.put("parentId", 0);
        MallCategoryEntity currentCategory = null;
        if (null != id) {
            currentCategory = categoryService.queryObject(id);
        }
        //获取子分类数据
        if (null != currentCategory && null != currentCategory.getId()) {
            params.put("parentId", currentCategory.getId());
            currentCategory.setSubCategoryList(categoryService.queryList(params));
        }
        resultObj.put("currentCategory", currentCategory);
        return AjaxResult.success(resultObj);
    }
}
